package edu.scut.luluteam.contextlib.collection;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;

import edu.scut.luluteam.contextlib.Tools.Repository;
import edu.scut.luluteam.contextlib.bean.ContextInfo;
import edu.scut.luluteam.contextlib.constant.AppHolder;

/**
 * @author Guan
 * @date Created on 2018/11/21
 */
public class ContextInfoManager {


    private static ContextInfoManager instance = null;
    private static final String TAG = "ContextInfoManager";

    public static ContextInfoManager getInstance() {
        if (instance == null) {
            synchronized (ContextInfoManager.class) {
                if (instance == null) {
                    instance = new ContextInfoManager();
                }
            }
        }
        return instance;
    }

    private ContextInfoManager() {
    }

    //==========================================================

    /**
     * 将采集的context信息封装起来，然后上报服务器
     *
     * @param latitude
     * @param longitude
     */
    public void postContextInfo(double latitude, double longitude) {
        postContextInfo("", -1, -1, -1, latitude, longitude);

    }

    public void postContextInfo(String itemId, int rating, long duration, int wordCount, double latitude, double longitude) {

        ContextInfo info = new ContextInfo();
        info.itemId = itemId;
        info.rating = rating;
        info.duration = duration;
        info.wordCount = wordCount;
        collectDeviceInfo(info);
        collectPackageInfo(AppHolder.appContext, info);
        collectOtherInfo(info);
        collectLocationInfo(latitude, longitude, info);

        //上报信息
        Repository.postContextInfo(info);

    }


    //============================================================

    /**
     * static：采集设备信息
     *
     * @param info
     */
    private void collectDeviceInfo(ContextInfo info) {
        ContextInfo.DeviceInfo deviceInfo = new ContextInfo.DeviceInfo();
        deviceInfo.brand = Build.BRAND;
        deviceInfo.display = Build.DISPLAY;
        deviceInfo.model = Build.MODEL;
        deviceInfo.SDKInt = Build.VERSION.SDK_INT;
        deviceInfo.manufacturer = Build.MANUFACTURER;
//        deviceInfo.simProvider;
        deviceInfo.versionRelease = Build.VERSION.RELEASE;
        info.deviceInfo = deviceInfo;
        Log.i(TAG, "收集设备信息");
    }


    /**
     * 采集package信息
     *
     * @param context
     * @param item
     */
    private void collectPackageInfo(Context context, ContextInfo item) {
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), PackageManager.GET_ACTIVITIES);
            if (pi != null) {
                String versionName = pi.versionName == null ? "null" : pi.versionName;
                String versionCode = pi.versionCode + "";

                item.versionName = versionName;
                item.versionCode = versionCode;
            }

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

    }


    /**
     * 采集其余信息
     *
     * @param info
     */
    private void collectOtherInfo(ContextInfo info) {

        info.timestamp = System.currentTimeMillis();

        Log.i(TAG, "收集Other信息");
    }

    /**
     * 设置 位置信息
     *
     * @param latitude
     * @param longitude
     */
    private void collectLocationInfo(double latitude, double longitude, ContextInfo info) {
        ContextInfo.LocationInfo locationInfo = new ContextInfo.LocationInfo();
        locationInfo.latitude = latitude;
        locationInfo.longitude = longitude;
        info.locationInfo = locationInfo;

    }
}
